package com.webrtc.demo.contacts

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.webrtc.demo.R
import com.webrtc.demo.util.ContactsUtils
import com.yzq.zxinglibrary.encode.CodeCreator
import kotlinx.android.synthetic.main.activity_my_name_card.*

class MyNameCardActivity : AppCompatActivity() {

    companion object {

        fun launch(context: Context) {
            context.startActivity(Intent(context, MyNameCardActivity::class.java))
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        title = "我的用户ID"
        setContentView(R.layout.activity_my_name_card)

        val myUid = ContactsUtils.getMyUid(this)
        nameTv.text = myUid
        copyBtn.setOnClickListener {
            val cm = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            val clip = ClipData.newPlainText("my_name_card", myUid)
            cm.setPrimaryClip(clip)
            Toast.makeText(this, "已复制", Toast.LENGTH_SHORT).show()
        }
        imageView.post {
            imageView.setImageBitmap(
                CodeCreator.createQRCode(
                    myUid,
                    imageView.width,
                    imageView.height,
                    null
                )
            )
        }
    }
}